#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Agresso.Interface.Layer.Domain;
using Agresso.Interface.Layer.Service;
using System.Collections;
using System.IO;
using System.Configuration;

#endregion

namespace Agresso.Interface.Layer.Web
{
    public partial class UserControlSoftmarVesselMapping : UserControl
    {
        User m_current_user;
        MainForm m_mainForm;
        string m_startDefault;
        VesselService m_vesselService;
        ProfitCentreService m_ProfitCentreService;
        Timer m_timerSearch = new Timer();


        public UserControlSoftmarVesselMapping(string name, User currentUser, MainForm mainForm)
        {
            InitializeComponent();
            labelTitle.Text = name;
            this.Name = name;
            m_current_user = currentUser;
            m_vesselService = new VesselService();
            m_ProfitCentreService = new ProfitCentreService();
            m_mainForm = mainForm;
            initColumnWidth();

            m_startDefault = ConfigurationManager.AppSettings["StartPath"];
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            toolBarButtonExcel.Click += new EventHandler(toolBarButtonExcel_Click);
            listViewAlert.DoubleClick += new EventHandler(listViewAlert_DoubleClick);
            toolBarButtonNew.Click += new EventHandler(toolBarButtonNew_Click);
            toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
           // toolBarButtonDelete.Enabled = false;
            toolBarButtonRefresh_Click(null, null);
            initTimerSearch();
        }

        void toolBarButtonDelete_Click(object sender, EventArgs e)
        {
            if (listViewAlert.SelectedItem == null)
            {
                MessageBox.Show("Please select the item want to delete.");
                return;
            }
            if (listViewAlert.SelectedItem.Tag == null) return;
            MessageBox.Show("Are sure to delete selected item?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question, new EventHandler(deleteVessel));
        }

        void deleteVessel(object sender, EventArgs e)
        {
            MessageBoxWindow x = (MessageBoxWindow)sender;
            if (x.DialogResult == DialogResult.Yes)
            {
                if (listViewAlert.SelectedItem == null) return;
                if (listViewAlert.SelectedItem.Tag == null) return;
                Vessel a = (Vessel)listViewAlert.SelectedItem.Tag;
                m_vesselService.DeleteVessel(a);
                listViewAlert.Items.RemoveAt(listViewAlert.SelectedItem.Index);
               // toolBarButtonDelete.Enabled = false;
            }
        }

        void toolBarButtonNew_Click(object sender, EventArgs e)
        {
            SoftmarVesselMappingForm adf = new SoftmarVesselMappingForm(this);
            adf.ShowDialog();
        }

        private void initColumnWidth()
        {
            listViewAlert.Columns[0].Width = 100;
            listViewAlert.Columns[1].Width = 72;
            listViewAlert.Columns[2].Width = 72;
            listViewAlert.Columns[3].Width = 100;
            listViewAlert.Columns[4].Width = 44;
            listViewAlert.Columns[5].Width = 79;
            listViewAlert.Columns[6].Width = 83;
            listViewAlert.Columns[7].Width = 83;
            listViewAlert.Columns[8].Width = 100;
            listViewAlert.Columns[9].Width = 100;
            listViewAlert.Columns[10].Width = 100;
            listViewAlert.Columns[11].Width = 50;
        }

        void listViewAlert_DoubleClick(object sender, EventArgs e)
        {
            if (listViewAlert.SelectedItem == null) return;
            if (listViewAlert.SelectedItem.Tag == null) return;
            Vessel a = (Vessel)listViewAlert.SelectedItem.Tag;
            SoftmarVesselMappingForm adf = new SoftmarVesselMappingForm(a, this);
            adf.ShowDialog();
        }

        void toolBarButtonExcel_Click(object sender, EventArgs e)
        {
            if (listViewAlert.Items.Count == 0)
            {
                MessageBox.Show("No Data to export, please refresh first.");
                return;
            }
            DataSet ds = new DataSet();
            DataTable dt = ds.Tables.Add("Table");
            foreach (ColumnHeader column in listViewAlert.Columns)
            {
                dt.Columns.Add(column.Text);
            }
            foreach (ListViewItem itm in listViewAlert.Items)
            {
                ArrayList row = new ArrayList();
                foreach (ColumnHeader column in listViewAlert.Columns)
                {
                    row.Add(itm.SubItems[column.Index].Text);
                }
                dt.Rows.Add(row.ToArray());
            }

            System.Web.UI.WebControls.DataGrid grid =
                  new System.Web.UI.WebControls.DataGrid();
            grid.HeaderStyle.Font.Bold = true;
            grid.HeaderStyle.Font.Name = "Calibri";
            grid.HeaderStyle.BackColor = Color.Gold;
            grid.Font.Name = "Calibri";
            grid.Font.Size = 9;
            grid.DataSource = ds;
            grid.DataMember = "Table";
            grid.DataBind();
            string filename = "reportSoftmar" + DateTime.Now.Second.ToString() + ".xls";
            string filePath = m_startDefault + filename;
            using (StreamWriter sw = new StreamWriter(filePath))
            {
                using (System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw))
                {
                    grid.RenderControl(hw);
                }
            }
            Link.Open(filename);
        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            listViewAlert.Items.Clear();
            statusBar1.Text = "Total : 0 item(s)";
            IList vesselMasters = m_vesselService.GetAllVesselMapping();
            LoadItems(vesselMasters);
        }

        private void LoadItems(IList vesselMasters)
        {

            foreach (Vessel a in vesselMasters)
            {
                a.ProfitCentre = m_ProfitCentreService.GetProfitCentreByID(a.ProfitCentre.profit_centre_id);
                ListViewItem item = listViewAlert.Items.Add(a.vessel_name);
                item.SubItems.Add(a.vessel_code);
                item.SubItems.Add(a.year_of_build);
                item.SubItems.Add(a.flag);
                item.SubItems.Add(a.dwt);
                item.SubItems.Add(a.charter_type);
                item.SubItems.Add(a.size_class);
                item.SubItems.Add(a.vessel_type);
                item.SubItems.Add(a.agr_description);
                item.SubItems.Add(a.agr_dim_value);
                item.SubItems.Add(a.agr_rel_value);
                item.SubItems.Add(a.client);
                item.SubItems.Add(a.ProfitCentre.profit_centre_id > 0 ? a.ProfitCentre.agr_description + " [" + a.ProfitCentre.agr_dim_value + "]" : "");
                item.Tag = a;
            }
            statusBar1.Text = string.Format("Total : {0} item(s)", vesselMasters.Count);
        }

        internal void UpdateSelectedItem(Vessel vessel)
        {
            if (listViewAlert.SelectedItem == null) return;
            if (listViewAlert.SelectedItem.Tag == null) return;
            listViewAlert.SelectedItem.SubItems[8].Text = vessel.agr_description;
            listViewAlert.SelectedItem.SubItems[9].Text = vessel.agr_dim_value;
            listViewAlert.SelectedItem.SubItems[10].Text = vessel.agr_rel_value;
            listViewAlert.SelectedItem.SubItems[12].Text = vessel.ProfitCentre.profit_centre_id > 0 ? vessel.ProfitCentre.agr_description + " [" + vessel.ProfitCentre.agr_dim_value + "]" : "";

            listViewAlert.SelectedItem.Tag = vessel;
        }

        private void textBoxSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                m_timerSearch.Start();
            }
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            Search();
        }

        private void Search()
        {
            listViewAlert.Items.Clear();
            statusBar1.Text = "Total : 0 item(s)";
            IList softmarAlert = m_vesselService.GetAllVesselMapping(textBoxSearch.Text);
            LoadItems(softmarAlert);
        }

        private void initTimerSearch()
        {
            m_timerSearch.Interval = 100;
            m_timerSearch.Start();
            m_timerSearch.Tick += new EventHandler(m_timerSearch_Tick);
        }

        void m_timerSearch_Tick(object sender, EventArgs e)
        {
            Search();
            m_timerSearch.Stop();
        }

        internal void InsertNewItem(Vessel a)
        {
            listViewAlert.SelectedItems.Clear();
            a.ProfitCentre = m_ProfitCentreService.GetProfitCentreByID(a.ProfitCentre.profit_centre_id);
            ListViewItem item = listViewAlert.Items.Add(a.vessel_name);
            item.SubItems.Add(a.vessel_code);
            item.SubItems.Add(a.year_of_build);
            item.SubItems.Add(a.flag);
            item.SubItems.Add(a.dwt);
            item.SubItems.Add(a.charter_type);
            item.SubItems.Add(a.size_class);
            item.SubItems.Add(a.vessel_type);
            item.SubItems.Add(a.agr_description);
            item.SubItems.Add(a.agr_dim_value);
            item.SubItems.Add(a.agr_rel_value);
            item.SubItems.Add(a.client);
            item.SubItems.Add(a.ProfitCentre.profit_centre_id > 0 ? a.ProfitCentre.agr_description + " [" + a.ProfitCentre.agr_dim_value + "]" : "");
            item.Tag = a;
            item.Selected = true;
        }

        //private void listViewAlert_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    toolBarButtonDelete.Enabled = true;
        //}
    }
}